import ajax from '/utils/request.js' //导入外部js
export default {
    state: {
        config: {},
        defaultTheme: '#5cbd9c', //默认主题色
        moldName: 'zhaopin', //当前模块名字
        /** @description 方法不牵扯到state中数据修改，仅用于处理页面中的逻辑 */
        getConfig: async (mod = 'siteConfig') => {
            let realMod = mod;
            let result = await ajax.get({
                url: `include/ajax.php?service=${realMod}&action=config`, //请求地址
            });
            let data = result.data;
            if (data.state == 100) {
                return data.info;
            } else {
                return {};
            }
        },
        hex2rgb(color = '#5CBD9C') {
            let hexs = null;
            let reg = /^\#?[0-9A-Fa-f]{6 || 3}$/;
            if (!reg.test(color)) return
            color = color.replace('#', '') // 去掉#
            if (color.length == 3) { // 如果是3位色值，则每个色值重复2次
                color = color.replace(/./g, '$&$&')
            }
            hexs = color.match(/../g) // 切割成数组 409EFF => ['40','9E','FF']
            // 将切割的色值转换为16进制
            for (let i = 0; i < hexs.length; i++) {
                hexs[i] = parseInt(hexs[i], 16)
            }
            return hexs.join(',') // 返回rgb色值[64, 158, 255]
        }
    },
    getters: {
        zhaopin_themeColor: async state => {
            let config = state.config;
            if (JSON.stringify(config) == '{}') {
                let res = await state.getConfig(state.moldName);
                state.config = res;
                config = res; //修改state.config
            }
            let themeColor = (config.themeColor || state.defaultTheme);
            return `--theme-main-color:${state.hex2rgb(themeColor)}`; //此值为空返回空字符串
        },

        zhaopin_config:async state => {
            let config = state.config;
            if (JSON.stringify(config) == '{}') {
                let res = await state.getConfig(state.moldName);
                state.config = res;
                config = res; //修改state.config
            }
            return config;

        }
    },
    mutations: {

    },
    actions: {},
}